package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.AnimationCategory;
import com.slg.android.manyanime.callBackInterface.OnClickCallBack;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by E540 on 2015/3/11.
 */
public class AnimeCategoryAdapter extends RecyclerView.Adapter<AnimeCategoryAdapter.CategoryViewHolder> {
    private Context context;
    private List<AnimationCategory> data;
    private OnClickCallBack listener;
    public final int TYPE_GROUP = 0;
    public final int TYPE_CATEGORY = 1;

    public void setOnItemClickListener(OnClickCallBack listener){
           this.listener = listener;
    }


    public AnimeCategoryAdapter(Context context, List<AnimationCategory> data) {
        this.context = context;
        this.data = data;
    }


    @Override
    public int getItemViewType(int position) {
        AnimationCategory category = data.get(position);
        if (category.isGroup()) {
            return TYPE_GROUP;
        } else {
            return TYPE_CATEGORY;
        }
    }


    @Override
    public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = null;
        if (viewType == TYPE_GROUP) {
            view = inflater.inflate(R.layout.item_animation_category_group, parent, false);
        } else {
            view = inflater.inflate(R.layout.item_animation_category, parent, false);
        }
        return new CategoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CategoryViewHolder holder, int position) {
        holder.listener = this.listener;
        AnimationCategory category = data.get(position);
        if (category.isGroup()) {
            holder.groupName.setText(category.getName());
        } else {
            holder.categoryName.setText(category.getName());
            holder.categoryImage.setImageResource(R.drawable.item_default);
            BitmapUtils utils = DataStore.getBitmapUtils();
            utils.configDefaultLoadingImage(R.drawable.item_default);
            utils.display(holder.categoryImage, category.getPic());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @ViewInject(R.id.item_anime_category_group_name)
        public TextView groupName;
        @ViewInject(R.id.item_anime_category_name)
        public TextView categoryName;
        @ViewInject(R.id.item_anime_category_image)
        public ImageView categoryImage;
        public OnClickCallBack listener;

        public CategoryViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClick(v, getPosition());
            }
        }
    }
}
